package com.szy.web.servlet;

import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.SmartFile;
import com.jspsmart.SmartRequest;
import com.jspsmart.SmartUpload;
import com.jspsmart.SmartUploadException;
import com.szy.web.dao.NewsDAO;
public class updateNewsServlet extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest request,
		HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		
	   if ( request.getSession().getAttribute("Login") != null) 
	   {
			String message = (String) request.getSession().getAttribute("Login");
			if (!message.equals("true"))
			{
				 //如果登录标志不为true则跳转回登录页面
				 response.sendRedirect("index.jsp");
				 return;
			}
			else
			{
				System.out.println("true");
				request.getSession().setAttribute("Login","true");
			}   
	   }
	   else
	   {
		   System.out.println("Nothing");
	    	response.sendRedirect("index.jsp");
	    	return;
	   } 
	   
	    ServletConfig config = getServletConfig();
		SmartUpload su= new SmartUpload();


		try 
		{
			su.initialize(config, request, response);
			su.setAllowedFilesList("gif,jpg,jpeg,png");
			su.setMaxFileSize(1024*1024*10);
			su.upload();
			Date date = new Date();
			String filename = "";
			SmartFile file = su.getFiles().getFile(0);
			if( !file.isMissing() )
			{
				filename = file.getFileName();
				
				@SuppressWarnings("deprecation")
				String yearString = date.getYear()+"";
				@SuppressWarnings("deprecation")
				String mountString = date.getMonth()+"";
				@SuppressWarnings("deprecation")
				String dayString = date.getDay()+"";
				@SuppressWarnings("deprecation")
				String hourString = date.getHours()+"";
				@SuppressWarnings("deprecation")
				String miniteString = date.getMinutes()+"";
				@SuppressWarnings("deprecation")
				String secString = date.getSeconds()+"";
				filename =yearString  +mountString+dayString+hourString+miniteString+secString+ filename;
				System.out.println(filename);
				file.saveAs("PIC/"+filename,SmartFile.SAVEAS_VIRTUAL);
			}
			SmartRequest req = su.getRequest();	
			int nid = Integer.parseInt(req.getParameter ( "nid" ));
		    String title = req.getParameter("newsTitle");
			System.out.println("title = " + title);
			String digest =req.getParameter ( "digest" );
			String body = req.getParameter ( "newsDetail" );
			String source =req.getParameter ( "source" );
			int deleted = Integer.parseInt(req.getParameter ( "deletechiose" ));
			int cid = Integer.parseInt(req.getParameter ( "category" ));
			System.out.println("okkkkkkk"+body);
		   try 
		   {
			   NewsDAO newsDAO = new NewsDAO();
			   if ( filename != "") 
			   {
				  
				   newsDAO.updateNews(nid, cid, title, digest, body, source, deleted);
			   }
				else
				{
					newsDAO.updateNews(nid, cid, title, digest, body, source, filename, deleted);
				}
				
			} catch (ClassNotFoundException e) 
			{
				System.out.println("修改失败");
				e.printStackTrace();
			}
		} 
		catch (SmartUploadException e)
		{
			System.out.println("修改失败");
			e.printStackTrace();
		}
		System.out.println("修改成功");
	    response.sendRedirect("ShowNews?method=view");
		   
//		String method = request.getParameter("method");
		
//		if (method.equalsIgnoreCase("updateNews")) {
//			String page = request.getParameter("currentPage");
//			int currentPage = 1;
//			int pageSize = 10;
//			if (page != null) {
//				currentPage = Integer.parseInt(page);
//			}
//			NewsDAO newsDAO;
//			try {
//				newsDAO = new NewsDAO();
//				Page pageModel = newsDAO.showNews(currentPage, pageSize);// 查询新闻从currPage到currPage+pageSize
//				request.setAttribute("pageModel", pageModel);
//				request.getSession ( ).setAttribute ( "Login", "true" );
//				request.getRequestDispatcher("showNews.jsp").forward(request,response);
//			} catch (ClassNotFoundException e) {
//				
//				e.printStackTrace();
//			}
//			
//		}
	
	}
	
}
